# import tkinter as tk
#
# def change_font():
#     selected_size = size_var.get()
#     label.config(font=("Helvetica", int(selected_size)))
#
# root = tk.Tk()
#
# # 字体大小变量
# size_var = tk.StringVar(root)
# size_var.set("14")
#
# # 选项菜单
# size_menu = tk.OptionMenu(root, size_var, "14", "42")
# size_menu.grid(row=0, column=0)  # 使用grid方法来布局
#
# # 改变字体按钮
# change_button = tk.Button(root, text="改变字体", command=change_font)
# change_button.grid(row=0, column=1)  # 使用grid方法来布局
#
# # 标签
# label = tk.Label(root, text="Hello", font=("Helvetica", 14))
# label.grid(row=0, column=2, columnspan=2)  # 使用grid方法来布局
#
# root.mainloop()



import tkinter as tk
import tkinter.scrolledtext as tst
from tkinter import filedialog, colorchooser

def rgb_to_hex(rgb):
    """将RGB颜色值转换为十六进制颜色代码"""
    return '#{:02x}{:02x}{:02x}'.format(*rgb)

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        # 创建文本编辑区域
        self.textEdit = tst.ScrolledText(self, width=80, height=20)
        self.textEdit.grid(row=0, column=0, sticky='nsew')

        # 定义一个标签用于设置文本颜色，初始设置为黑色
        self.textEdit.tag_config("color", foreground="black")
        self.textEdit.tag_add("color", "1.0", tk.END)

        # 创建按钮面板
        self.btnPanel = tk.Frame(self)
        self.btnPanel.grid(row=0, column=1, sticky='ns')

        # 创建打开按钮
        self.btnOpen = tk.Button(self.btnPanel, text='打开', command=self.funcOpen)
        self.btnOpen.pack(fill='x', expand=True)

        # 创建保存按钮
        self.btnSave = tk.Button(self.btnPanel, text='保存', command=self.funcSave)
        self.btnSave.pack(fill='x', expand=True)

        # 创建颜色选择按钮
        self.btnColor = tk.Button(self.btnPanel, text='颜色', command=self.funcColor)
        self.btnColor.pack(fill='x', expand=True)

        # 创建退出按钮
        self.btnExit = tk.Button(self.btnPanel, text='退出', command=self.master.destroy)
        self.btnExit.pack(fill='x', expand=True)

    def funcOpen(self):
        filepath = filedialog.askopenfilename()
        if filepath:
            with open(filepath, 'r') as file:
                content = file.read()
                self.textEdit.delete('1.0', tk.END)
                self.textEdit.insert('1.0', content)

    def funcSave(self):
        filepath = filedialog.asksaveasfilename()
        if filepath:
            with open(filepath, 'w') as file:
                content = self.textEdit.get('1.0', tk.END)
                file.write(content)

    def funcColor(self):
        color = colorchooser.askcolor(title="选择文本颜色")
        if color[1]:  # color[1] 是实际的颜色值，color[0] 是颜色的十六进制字符串
            hex_color = rgb_to_hex(color[0])  # 转换为十六进制颜色代码
            self.textEdit.tag_config("color", foreground=hex_color)

root = tk.Tk()
app = Application(master=root)
app.mainloop()